package com.tkjt.pms.modules.manageWeight.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tkjt.pms.common.entity.TkIndexInfo;
import com.tkjt.pms.common.entity.TkIndexWeight;
import com.tkjt.pms.common.entity.TkIndexWeightLog;
import com.tkjt.pms.common.utils.EsDateUtils;
import com.tkjt.pms.common.utils.MyBeanUtils;
import com.tkjt.pms.modules.kpiAudit.model.kpiAuditrowList.YearModel;
import com.tkjt.pms.modules.manageWeight.mapper.WeightMapper;
import com.tkjt.pms.modules.manageWeight.model.TkIndexWeightEntityVO;
import com.tkjt.pms.modules.manageWeight.model.TkIndexWeightModel;
import com.tkjt.pms.modules.manageWeight.service.WeightLogService;
import com.tkjt.pms.modules.manageWeight.service.WeightService;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.entity.SysDictItem;
import org.jeecg.modules.system.service.ISysDictItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Slf4j
@Transactional
@Service
public class WeightServiceImpl extends ServiceImpl<WeightMapper, TkIndexWeight> implements WeightService {
    final
    WeightLogService weightLogService;

    public WeightServiceImpl(WeightLogService weightLogService) {
        this.weightLogService = weightLogService;
    }
    @Autowired
    ISysDictItemService sysDictItemService;
    //1  一线单位  2 养康本部
    @Override
    public TkIndexWeightModel getYixianAndBenBuWeightList(HttpServletRequest request,String type) {
        TkIndexWeightModel model=new TkIndexWeightModel();
        model.setDefaulYearMonth(EsDateUtils.getIndexYearMonth(new Date()));
        model.setYearMonthList(getYearList());
        model.setIndexCodeList(getIndexCodeList(type));
        model.setOrgCodeList(getOrgCodeList(type));
        model.setTkIndexWeightEntityVOList(getVoList(request,type,model));
        return model;
    }

    @Override
    public String saveRows(HttpServletRequest request,TkIndexWeightEntityVO vo) {
        String message = null;
                if (StringUtils.isNotEmpty(vo.getId())) {
                    if(vo.getWeightMonth()!=null && !"".equals(vo.getWeightMonth())){
                        //weightMonth; //当前月份  代替  createBy
                        //currentWeight;//当前月份权重 代替 updateBy
                        if(vo.getWeightFormat()==1){  //判断百分比
                            boolean contains = vo.getCurrentWeight().contains("%");
                            if(!contains){
                                vo.setCurrentWeight(vo.getCurrentWeight()+"%");
                            }
                        }
                        if(vo.getWeightFormat()==2){	//判断是区间
                            boolean contains = vo.getCurrentWeight().contains("[");
                            if(!contains){
                                vo.setCurrentWeight("["+vo.getCurrentWeight()+"]");
                            }
                        }
                        switch (vo.getWeightMonth()) {
                            case "01":
                                vo = addWeightT01(vo,vo.getCurrentWeight());
                                break;
                            case "02":
                                vo = addWeightT02(vo,vo.getCurrentWeight());
                                break;
                            case "03":
                                vo = addWeightT03(vo,vo.getCurrentWeight());
                                break;
                            case "04":
                                vo = addWeightT04(vo,vo.getCurrentWeight());
                                break;
                            case "05":
                                vo = addWeightT05(vo,vo.getCurrentWeight());
                                break;
                            case "06":
                                vo = addWeightT06(vo,vo.getCurrentWeight());
                                break;
                            case "07":
                                vo = addWeightT07(vo,vo.getCurrentWeight());
                                break;
                            case "08":
                                vo = addWeightT08(vo,vo.getCurrentWeight());
                                break;
                            case "09":
                                vo = addWeightT09(vo,vo.getCurrentWeight());
                                break;
                            case "10":
                                vo = addWeightT10(vo,vo.getCurrentWeight());
                                break;
                            case "11":
                                vo = addWeightT11(vo,vo.getCurrentWeight());
                                break;
                            case "12":
                                vo  = addWeightT12(vo,vo.getCurrentWeight());
                                break;
                            default:
                                break;
                        }
                        //page.setCurrentWeight(null);
                    }
                    TkIndexWeight t=baseMapper.selectOne(new QueryWrapper<TkIndexWeight>().eq("id",vo.getId()));
//                    TkIndexWeightEntity t =tkIndexWeightService.get(TkIndexWeightEntity.class, vo.getId());
//                    t.setUpdateBy(ResourceUtil.getSessionUser().getUserName());
                    t.setUpdateDate(new Date());
                    try {
                        message = "TkIndexWeight例子更新成功";
                        MyBeanUtils.copyBeanNotNull2Bean(vo, t);
                        String getweightY = t.getWeightY();
                        if(getweightY.length()>4){
                            t.setWeightY(getweightY.substring(0, 4));
                        }
//                        t.setCreateBy(ResourceUtil.getSessionUser().getUserName());
                        baseMapper.updateById(t);
//                        tkIndexWeightService.saveOrUpdate(t);
                        TkIndexWeightLog entityLog = new TkIndexWeightLog();
                        MyBeanUtils.copyBeanNotNull2Bean(t, entityLog);
                        entityLog.setweightId(t.getId());
                        entityLog.setId(null);
                        weightLogService.save(entityLog);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } else {
                    try {
                        TkIndexWeight t=new TkIndexWeight();
                        message = "TkIndexWeight例子添加成功";
                        MyBeanUtils.copyBeanNotNull2Bean(vo, t);
                        baseMapper.insert(t);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }
        return message;
    }

    @Override
    public void updateLastMonthWeightByParam(Map<String, Object> map) {
        this.updateLastMonthWeightByParam(map);
    }


    private List<YearModel> getYearList(){
        List<SysDictItem> typeList=sysDictItemService.selectItemsByMainId("year");
        List<YearModel> list=new ArrayList<>();
        for (SysDictItem t:typeList){
            YearModel model=new YearModel();
            model.setOption(t.getItemText());
            model.setValue(t.getItemValue());
            list.add(model);
        }
//        List<SysType> typeList=baseMapper.getTsTypeByCode("year");
//        List<YearModel> list=new ArrayList<>();
//        for (SysType t:typeList){
//            YearModel model=new YearModel();
//            model.setOption(t.getTypename());
//            model.setValue(t.getTypecode());
//            list.add(model);
//        }
        return list;
    }

    /**
     *
     * @param type 1  一线单位  2 养康本部
     */
    private List<YearModel> getIndexCodeList(String type){
        String indexGroup;
        if("1".equals(type)){
            indexGroup="2";// 一线单位
        }else{
            indexGroup="1";// 养康本部
        }
        List<TkIndexInfo> indexInfoList=baseMapper.getSelectTkIndexInfo(indexGroup);
        List<YearModel> list=new ArrayList<>();
        for(TkIndexInfo t:indexInfoList){
            YearModel model=new YearModel();
            model.setOption(t.getIndexName());
            model.setValue(t.getIndexCode());
            list.add(model);
        }
        return  list;
    }

    private List<YearModel> getOrgCodeList(String type){
        String orgCode;
        if("1".equals(type)){
            orgCode="A01A01A01A";// 一线单位
        }else{
            orgCode="A01A01A02A";// 养康本部
        }
        List<SysDepart> indexInfoList=baseMapper.getSelectTSDepart(orgCode);
        List<YearModel> list=new ArrayList<>();
        for(SysDepart t:indexInfoList){
            YearModel model=new YearModel();
            model.setOption(t.getDepartName());
            model.setValue(t.getOrgCode());
            list.add(model);
        }
        return  list;
    }

    private List<TkIndexWeightEntityVO> getVoList(HttpServletRequest request,String type,TkIndexWeightModel model){
        String likeOrgCode;
        if("1".equals(type)){
            likeOrgCode="A01A01A01A";// 一线单位
        }else{
            likeOrgCode="A01A01A02A";// 养康本部
        }
        String weightY=request.getParameter("yearmonth");
        String indexCode=request.getParameter("indexCode");//指标名称
        String orgCode=request.getParameter("orgCode");//权重机构
        TkIndexWeight tkIndexWeight=new TkIndexWeight();
        tkIndexWeight.setWeightY(weightY);
        if(StringUtils.isNotEmpty(indexCode)){
            tkIndexWeight.setIndexCode(indexCode);
        }
        if(StringUtils.isNotEmpty(orgCode)){
            tkIndexWeight.setOrgCode(orgCode);
        }
        String moth="";
        String year;
        if (tkIndexWeight.getWeightY() != null && !"".equals(tkIndexWeight.getWeightY())) {
            moth = tkIndexWeight.getWeightY().substring(4, 6);
            year = tkIndexWeight.getWeightY().substring(0, 4);
            tkIndexWeight.setWeightY(year);
        } else {
            tkIndexWeight.setWeightY("YNULL");
        }
        tkIndexWeight.setUpdateBy(null);

        List<TkIndexWeight> list =baseMapper.getTkIndexWeightByYixian(tkIndexWeight,likeOrgCode);
        List<TkIndexWeightEntityVO> list2 = new ArrayList<>();
        for (TkIndexWeight aList : list) {
            TkIndexWeightEntityVO vo = new TkIndexWeightEntityVO();
            try {
                BeanUtils.copyProperties(vo, aList);
                list2.add(vo);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        Map<String,String> orgCodeMap=listToMap(model.getOrgCodeList());
        Map<String,String> indexCodeMap=listToMap(model.getIndexCodeList());
        for(TkIndexWeightEntityVO item : list2) {
            item.setIndexCodeName(indexCodeMap.get(item.getIndexCode()));
            item.setOrgCodeName(orgCodeMap.get(item.getOrgCode()));
            if(tkIndexWeight.getWeightY()!=null && !"".equals(tkIndexWeight.getWeightY())){
                TkIndexWeightLog findByPropertyisOrder=baseMapper.getTkIndexWeightLogByWeightId(item.getId());
                if(findByPropertyisOrder!=null){
                    Date updateDate = findByPropertyisOrder.getUpdateDate();
                    long now = new Date().getTime();
                    long updatetimer = updateDate.getTime();
                    long tr = now-updatetimer;
                    if(tr<3600000L){	//如果修改时间小于1小时
                        request.setAttribute("isHourUpdate", 1);
                    }else{
                        request.setAttribute("isHourUpdate", 2);
                    }
                }
                switch (moth) {
                    case "01":
                        item.setCurrentWeight(item.getWeightM01());
                        item.setWeightMonth("01");
                        break;
                    case "02":
                        item.setCurrentWeight(item.getWeightM02());
                        item.setWeightMonth("02");
                        break;
                    case "03":
                        item.setCurrentWeight(item.getWeightM03());
                        item.setWeightMonth("03");
                        break;
                    case "04":
                        item.setCurrentWeight(item.getWeightM04());
                        item.setWeightMonth("04");
                        break;
                    case "05":
                        item.setCurrentWeight(item.getWeightM05());
                        item.setWeightMonth("05");
                        break;
                    case "06":
                        item.setCurrentWeight(item.getWeightM06());
                        item.setWeightMonth("06");
                        break;
                    case "07":
                        item.setCurrentWeight(item.getWeightM07());
                        item.setWeightMonth("07");
                        break;
                    case "08":
                        item.setCurrentWeight(item.getWeightM08());
                        item.setWeightMonth("08");
                        break;
                    case "09":
                        item.setCurrentWeight(item.getWeightM09());
                        item.setWeightMonth("09");
                        break;
                    case "10":
                        item.setCurrentWeight(item.getWeightM10());
                        item.setWeightMonth("10");
                        break;
                    case "11":
                        item.setCurrentWeight(item.getWeightM11());
                        item.setWeightMonth("11");
                        break;
                    case "12":
                        item.setCurrentWeight(item.getWeightM12());
                        item.setWeightMonth("12");
                        break;
                    default:
                        break;
                }
                item.setweightY(item.getweightY()+"年"+moth+"月");
            }else{
                item.setUpdateBy(null);
            }
            if(item.getWeightFormat()==1){
                item.setWfomat("百分比(%),如:50%");
            }else if(item.getWeightFormat()==2){
                item.setWfomat("区间([a,b]),如:[-1,1]");
            }else{
                item.setWfomat("/");
            }
        }
        return list2;
    }

    private Map<String,String> listToMap(List<YearModel> list){
        Map<String,String> map=new HashMap<>();
        for (YearModel y:list){
            map.put(y.getValue(),y.getOption());
        }
        return  map;
    }





    public TkIndexWeightEntityVO addWeightT01(TkIndexWeightEntityVO tkIndexWeight,String str) {
        tkIndexWeight.setWeightM01(str);
        tkIndexWeight.setWeightM02(str);
        tkIndexWeight.setWeightM03(str);
        tkIndexWeight.setWeightM04(str);
        tkIndexWeight.setWeightM05(str);
        tkIndexWeight.setWeightM06(str);
        tkIndexWeight.setWeightM07(str);
        tkIndexWeight.setWeightM08(str);
        tkIndexWeight.setWeightM09(str);
        tkIndexWeight.setWeightM10(str);
        tkIndexWeight.setWeightM11(str);
        tkIndexWeight.setWeightM12(str);
        return tkIndexWeight;
    }


    public TkIndexWeightEntityVO addWeightT02(TkIndexWeightEntityVO tkIndexWeight,String str) {
        tkIndexWeight.setWeightM02(str);
        tkIndexWeight.setWeightM03(str);
        tkIndexWeight.setWeightM04(str);
        tkIndexWeight.setWeightM05(str);
        tkIndexWeight.setWeightM06(str);
        tkIndexWeight.setWeightM07(str);
        tkIndexWeight.setWeightM08(str);
        tkIndexWeight.setWeightM09(str);
        tkIndexWeight.setWeightM10(str);
        tkIndexWeight.setWeightM11(str);
        tkIndexWeight.setWeightM12(str);
        return tkIndexWeight;
    }


    public TkIndexWeightEntityVO addWeightT03(TkIndexWeightEntityVO tkIndexWeight,String str) {
        tkIndexWeight.setWeightM03(str);
        tkIndexWeight.setWeightM04(str);
        tkIndexWeight.setWeightM05(str);
        tkIndexWeight.setWeightM06(str);
        tkIndexWeight.setWeightM07(str);
        tkIndexWeight.setWeightM08(str);
        tkIndexWeight.setWeightM09(str);
        tkIndexWeight.setWeightM10(str);
        tkIndexWeight.setWeightM11(str);
        tkIndexWeight.setWeightM12(str);
        return tkIndexWeight;
    }


    public TkIndexWeightEntityVO addWeightT04(TkIndexWeightEntityVO tkIndexWeight,String str) {
        tkIndexWeight.setWeightM04(str);
        tkIndexWeight.setWeightM05(str);
        tkIndexWeight.setWeightM06(str);
        tkIndexWeight.setWeightM07(str);
        tkIndexWeight.setWeightM08(str);
        tkIndexWeight.setWeightM09(str);
        tkIndexWeight.setWeightM10(str);
        tkIndexWeight.setWeightM11(str);
        tkIndexWeight.setWeightM12(str);
        return tkIndexWeight;
    }


    public TkIndexWeightEntityVO addWeightT05(TkIndexWeightEntityVO tkIndexWeight,String str) {
        tkIndexWeight.setWeightM06(str);
        tkIndexWeight.setWeightM07(str);
        tkIndexWeight.setWeightM08(str);
        tkIndexWeight.setWeightM09(str);
        tkIndexWeight.setWeightM10(str);
        tkIndexWeight.setWeightM11(str);
        tkIndexWeight.setWeightM12(str);
        return tkIndexWeight;
    }


    public TkIndexWeightEntityVO addWeightT06(TkIndexWeightEntityVO tkIndexWeight,String str) {
        tkIndexWeight.setWeightM06(str);
        tkIndexWeight.setWeightM07(str);
        tkIndexWeight.setWeightM08(str);
        tkIndexWeight.setWeightM09(str);
        tkIndexWeight.setWeightM10(str);
        tkIndexWeight.setWeightM11(str);
        tkIndexWeight.setWeightM12(str);
        return tkIndexWeight;
    }


    public TkIndexWeightEntityVO addWeightT07(TkIndexWeightEntityVO tkIndexWeight,String str) {
        tkIndexWeight.setWeightM07(str);
        tkIndexWeight.setWeightM08(str);
        tkIndexWeight.setWeightM09(str);
        tkIndexWeight.setWeightM10(str);
        tkIndexWeight.setWeightM11(str);
        tkIndexWeight.setWeightM12(str);
        return tkIndexWeight;
    }


    public TkIndexWeightEntityVO addWeightT08(TkIndexWeightEntityVO tkIndexWeight,String str) {
        tkIndexWeight.setWeightM08(str);
        tkIndexWeight.setWeightM09(str);
        tkIndexWeight.setWeightM10(str);
        tkIndexWeight.setWeightM11(str);
        tkIndexWeight.setWeightM12(str);
        return tkIndexWeight;
    }


    public TkIndexWeightEntityVO addWeightT09(TkIndexWeightEntityVO tkIndexWeight,String str) {
        tkIndexWeight.setWeightM09(str);
        tkIndexWeight.setWeightM10(str);
        tkIndexWeight.setWeightM11(str);
        tkIndexWeight.setWeightM12(str);
        return tkIndexWeight;
    }


    public TkIndexWeightEntityVO addWeightT10(TkIndexWeightEntityVO tkIndexWeight,String str) {
        tkIndexWeight.setWeightM10(str);
        tkIndexWeight.setWeightM11(str);
        tkIndexWeight.setWeightM12(str);
        return tkIndexWeight;
    }


    public TkIndexWeightEntityVO addWeightT11(TkIndexWeightEntityVO tkIndexWeight,String str) {
        tkIndexWeight.setWeightM11(str);
        tkIndexWeight.setWeightM12(str);
        return tkIndexWeight;
    }


    public TkIndexWeightEntityVO addWeightT12(TkIndexWeightEntityVO tkIndexWeight,String str) {
        tkIndexWeight.setWeightM12(str);
        return tkIndexWeight;
    }



}
